<?php
/**
 * Name: AdminRoleController.class.php
 * Description:
 * User: DIY
 * Date: 2016/9/8
 * Time: 8:56
 */

namespace Admin\Controller;


use Common\Model\AdminAuthModel;
use Common\Model\AdminRoleModel;
use Common\Logic\AdminAuthLogic;
use Admin\Library\BootstrapPage;

class AdminRoleController extends BaseController{

    /*
     * 列表
     * */
    public function index(){
        //搜索数据
        $data = array(
            '1'=>'1',
        );
        if(I('get.name')){
            $data['name'] = array('like','%'.I('get.name').'%');
        }
        if(I('get.status')!=''){
            $data['status'] = I('get.status');
        }
        $AdminRoleModel = new AdminRoleModel();
        $count = $AdminRoleModel->where($data)->count();
        $Page = new BootstrapPage($count,20);
        $list['list'] = $AdminRoleModel->where($data)->limit($Page->firstRow.','.$Page->listRows)->select();
        $list['page'] = $Page->show();// 分页显示输出
        $this->assign('list',$list);
        $this->display();
    }

    /*
     * 新增
     * */
    public function add(){
        if(IS_POST){
            $AdminRoleModel = new AdminRoleModel();
            if($AdminRoleModel->create()){
                $result = $AdminRoleModel->add();
                if($result){
                    $this->success('保存成功',U('Admin/AdminRole/index'));
                }
                else{
                    $this->error('保存失败');
                }
            }
            $this->error('保存失败');
        }
        else{
            $this->display();
        }
    }
    /*
     * 编辑
     * */
    public function edit($id){
        $AdminRoleModel = new AdminRoleModel();
        if(IS_POST){
            if($AdminRoleModel->create()){
                $result = $AdminRoleModel->save();
                if($result){
                    $this->success('保存成功',U('Admin/AdminRole/index'));
                }
                else{
                    $this->error('保存失败');
                }
            }
            $this->error('保存失败');
        }
        else{
            $role = $AdminRoleModel->where(array('id'=>$id))->find();
            $this->assign('role',$role);
            $this->display();
        }
    }
    /*
     * 授权
     * */
    public function rule($id){
        $AdminAuthModel = new AdminAuthModel();
        $AdminRoleModel = new AdminRoleModel();
        if(IS_POST){
            if($AdminRoleModel->create()){
                $result = $AdminRoleModel->save();
                if($result){
                    $this->success('保存成功',U('Admin/AdminRole/index'));
                }
                else{
                    $this->error('保存失败');
                }
            }
            $this->error('保存失败');
        }
        else{
            $rules = $AdminRoleModel->field('rules')
                ->where(array('id'=>$id))
                ->find();
            if(empty($rules['rules'])){
                $rules['rules'] = '0';
            }

            $menu = $AdminAuthModel->field('p1.*,p2.id as checked ')
                ->alias('p1')
                ->join('LEFT JOIN (select * from __ADMIN_AUTH__ where id in ('.$rules['rules'].') ) as p2 on p1.id=p2.id')
                ->where(array('p1.hide'=>0))
                ->order('p1.sort desc')
                ->select();
            //$menu = $AdminAuthModel->where(array('hide'=>0))->select();
            $this->assign('menu',$menu);
            $this->display();
        }
    }
    /*
     * 删除
     * */
    public function del($id){
        $AdminRoleModel = new AdminRoleModel();
        $AdminRoleModel->delete($id);
        $this->success('删除成功',U('Admin/AdminRole/index'));
    }
}